package com.bigtotoro.shuiyincamera.video.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.bigtotoro.shuiyincamera.R;

public class MusicCropView extends View {
    public static final String TAG = "music-crop-view";

    public MusicCropView(Context context) {
        super(context);
        init(context);
    }

    public MusicCropView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MusicCropView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public MusicCropView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private int tapW = 20, tapH = 130;
    private int TAP_MIN_SPACE = 100;//tap之间最小距离
    private int txtTop = 20, tapTop = 30;
    private float lastX, lastY;
    private int tapedIndex = -1;
    private RectF leftTapRect, rightTapRect;
    private Bitmap leftTapB, rightTopB;
    private Rect tapBitmapRect = new Rect();

    private int duration = 12000;//单位毫秒, 12s
    private Paint leftPaint, rightPaint;

    private void init(Context context){

        leftTapB = BitmapFactory.decodeResource(context.getResources(), R.mipmap.drag_left);
        rightTopB = BitmapFactory.decodeResource(context.getResources(), R.mipmap.drag_right);
        tapBitmapRect.set(0, 0, leftTapB.getWidth(), leftTapB.getHeight());
//        tapW = tapBitmap.getWidth()/2;
//        tapH = tapBitmap.getHeight()/2;
        Log.d(TAG, "bitmap w "+leftTapB.getWidth()+", h "+leftTapB.getHeight());
        Log.d(TAG, "view w "+getWidth()+", h "+getHeight());

        leftPaint = new Paint();
        leftPaint.setColor(Color.parseColor("#FF2D55"));
        leftPaint.setStyle(Paint.Style.FILL);
        leftPaint.setStrokeWidth(14);
        leftPaint.setTextSize(26);
        leftPaint.setTextAlign(Paint.Align.LEFT);

        rightPaint = new Paint();
        rightPaint.setColor(Color.parseColor("#FF2D55"));
        rightPaint.setStyle(Paint.Style.FILL);
        rightPaint.setStrokeWidth(14);
        rightPaint.setTextSize(26);
        rightPaint.setTextAlign(Paint.Align.RIGHT);
    }


    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        int w = getWidth();
        int h = getHeight();
        if (w <= 0 || h <= 0)
            return;

        if(leftTapRect==null||rightTapRect==null){
            leftTapRect = new RectF(0, tapTop, tapW, tapH+tapTop);
            //leftTapRect = new RectF(0, 0, tapBitmap.getWidth(), tapBitmap.getHeight());
            rightTapRect = new RectF(w-tapW, tapTop, w, tapH+tapTop);
        }

        canvas.drawBitmap(leftTapB, tapBitmapRect, leftTapRect, null);
        canvas.drawBitmap(rightTopB, tapBitmapRect, rightTapRect, null);
//        canvas.drawText(1+"s", 100, 20, txtPaint);
        if(duration>0){
            int leftTime = (int) (leftTapRect.left*duration/(w*1000));
            canvas.drawText(leftTime+"s", leftTapRect.left, txtTop, leftPaint);
//            canvas.drawText(leftTime+"s", 100, 20, txtPaint);
            int rightTime = (int) (rightTapRect.right*duration/(w*1000));
            canvas.drawText(rightTime+"s", rightTapRect.right, txtTop, rightPaint);
        }

    }


    /**
     * 触摸事件处理
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean ret = super.onTouchEvent(event);// 是否向下传递事件标志 true为消耗
        int action = event.getAction();
        float x = event.getX();
        float y = event.getY();
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                tapedIndex = isSelectedTapControl(x, y);
                if (tapedIndex > -1) {
                    ret = true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if(tapedIndex > -1){
                    transTap(x-lastX, y-lastY);
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                tapedIndex = - 1;
                break;
        }

        lastX = x;
        lastY = y;
        return ret;
    }

    private void transTap(float dx, float dy){
        int width = getWidth();
        if(tapedIndex==0){
            if((leftTapRect.left+dx)<0){
                return;
            }
            if(rightTapRect.left - (leftTapRect.left + dx + tapW)<=100){
                return;
            }
            leftTapRect.left += dx;
            leftTapRect.right = leftTapRect.left+tapW;
            if(dragListener!=null&&duration>0){
                dragListener.onDrag(0, (int) (leftTapRect.left*duration/getWidth()), (int) (rightTapRect.right*duration/getWidth()));
            }
        }else if(tapedIndex==1){
            if((rightTapRect.right + dx)>width){
                return;
            }
            if(((rightTapRect.left+dx) - leftTapRect.right)<=100){
                return;
            }
            rightTapRect.left += dx;
            rightTapRect.right = rightTapRect.left+tapW;
            if(dragListener!=null&&duration>0){
                dragListener.onDrag(1, (int) (leftTapRect.left*duration/getWidth()), (int) (rightTapRect.right*duration/getWidth()));
            }
        }
        invalidate();
    }



    private static final void translateRect(RectF rect, float dx, float dy) {
        rect.left += dx;
        rect.right += dx;
    }

    private int isSelectedTapControl(float x, float y) {
        if (new RectF(leftTapRect.left-tapW/2, leftTapRect.top, leftTapRect.right+tapW/2, leftTapRect.bottom).contains(x, y))
            return 0;
        if (new RectF(rightTapRect.left-tapW/2, rightTapRect.top, rightTapRect.right+tapW/2, rightTapRect.bottom).contains(x, y))
            return 1;
        return -1;
    }

    public void setDuration(int duration){
        this.duration = duration;
        invalidate();
    }

    public int getLeftTime(){
        return (int) (leftTapRect.left*duration/getWidth());
    }

    public int getRightTime(){
        return (int) (rightTapRect.right*duration/getWidth());
    }

    public static interface OnMusicDragListener{
        void onDrag(int type, int left, int right);
    }

    private OnMusicDragListener dragListener;
    public void setOnMusicDragListener(OnMusicDragListener dragListener){
        this.dragListener = dragListener;
    }

}
